#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
typedef struct tag
{
    tag *lnode;
    tag *rnode;
    int data;
} nod_t;
int post[30], mid[30];
nod_t *create(int postl, int postr, int midl, int midr)
{
    //递归边界
    if (postl == postr || midl == midr)
    {
        nod_t *nod = (nod_t *)calloc(1, sizeof(nod_t));
        nod->data = post[postr];
        return nod;
    }
    else
    {
        int pos = 0;
        int i;
        for (i = midl; i <= midr; ++i)
        { //Find the index of root from inorder traversal
            if (mid[i] == post[postr])
            {
                pos = i;
                break;
            }
        }
        nod_t *nod = (nod_t *)calloc(1, sizeof(nod_t));
        nod->data = post[postr];
        if (pos != midl)
        {
            nod->lnode = create(postl, postr - (midr - pos) - 1, midl, pos - 1);
        }
        if (pos != midr)
        {
            nod->rnode = create(postr - (midr - pos), postr - 1, pos + 1, midr);
        }
        return nod;
    }
}
void level(nod_t *root)
{
    if (root)
    {
        queue<nod_t *> que;
        que.push(root);
        while (!que.empty())
        {
            if (que.front()->lnode != NULL)
            {
                que.push(que.front()->lnode);
            }
            if (que.front()->rnode != NULL)
            {
                que.push(que.front()->rnode);
            }
            printf("%d", que.front()->data);
            if (que.size() != 1)
            {
                putchar(' ');
            }
            que.pop();
        }
        putchar('\n');
    }
}
int main()
{
    int N;
    scanf("%d", &N);
    if(0==N)
    {
        return 0;
    }
    int i;
    for (i = 0; i < N; ++i)
    {
        scanf("%d", post + i);
    }
    for (i = 0; i < N; ++i)
    {
        scanf("%d", mid + i);
    }
    nod_t *root = create(0, N - 1, 0, N - 1);
    level(root);
    return 0;
}
